<template>
	<view class="publish-sign-container">
		<view style="margin-bottom: 60rpx;">
			<text class="txt">请选择为哪则招募信息发布签到活动：</text>
			<uni-section class="section">
				<uni-data-select v-model="value" :localdata="list"></uni-data-select>
			</uni-section>
		</view>
		<view>
			<text class="txt">选择签到位置</text>
			<input @click="getAddress" v-model="address" :disabled="true" type="text" placeholder="请选择"
				placeholder-style="font-size:12px;">
		</view>
		<view class="tip">
			签到位置经纬：{{longitude}}
		</view>
		<view class="tip">
			签到位置纬度：{{latitude}}
		</view>
		<button @click="publishClock" class="btn">发布签到</button>
	</view>
</template>

<script>
	import {
		getInfo
	} from "../../ajax/api/home.js";
	import {publishClock} from "../../ajax/api/clock.js";
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				list: [], //招募信息
				value: '', //选中的招募信息
				address: "", //地址
				latitude: "", //纬度
				longitude: "", //经度
			}
		},
		computed: {
			...mapState(["qiyeData"])
		},
		async onShow() {
			// #ifdef MP-WEIXIN
			if (wx.hideHomeButton) {
				wx.hideHomeButton();
			}
			// #endif
			const res = await getInfo({
				current: this.current,
				pageSize: this.pageSize,
				enterprise: this.qiyeData._id
			})
			const arr = res.rows;
			arr.forEach((item) => {
				this.list.push({
					value: item._id,
					text: item.title
				})
			})
		},
		methods: {
			getAddress() {
				const that = this;
				// 进行位置授权
				uni.authorize({
					scope: 'scope.userLocation',
					success(res) {
						// 选择位置
						uni.chooseLocation({
							success: (res) => {
								that.address = res.address;
								that.latitude = res.latitude;
								that.longitude = res.longitude;
							},
							fail(error) {
								console.log('选择位置失败', error)
							},
						})
					}
				})
			},
			// 发布打卡
			async publishClock(){
				if(!this.value){
					uni.showToast({
						title:"还未选择招募信息",
						icon:"error"
					})
				}else if(!this.address){
					uni.showToast({
						title:"未选择签到位置",
						icon:"error"
					})
				}else{
					const obj = {
						isClockIn:true, //是否开启打卡
						longitude:this.longitude,
						latitude:this.latitude
					}
					const res = await publishClock(this.value,obj);
					uni.showToast({
						title:"发布签到成功"
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:"/pages/welcome/welcome"
						});
					},500)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.publish-sign-container {
		padding: 40rpx;
	}

	.section {
		margin-bottom: 40rpx;
	}

	.txt {
		display: block;
		border-left: 2px solid $base-color;
		padding: 0 20rpx;
		margin: 40rpx 0;
	}

	input {
		padding: 12rpx;
		border: 1px solid #e5e5e5;
		border-radius: 4px;
	}

	.tip {
		margin: 40rpx 0;
	}

	.btn {
		color: #fff;
		background: $base-color;
		margin-top: 140rpx;
	}
</style>
